//
//  JSModaContentView.m
//  JSBasicFramework
//
//  Created by Apple on 2019/9/3.
//  Copyright © 2019 Apple. All rights reserved.
//

#import "JSModaContentView.h"

@interface JSModaContentView ()

@property (nonatomic, strong) UIView *contenView;
@property (nonatomic, strong) QMUIButton *cancelBtn;
@property (nonatomic, strong) UIView *lineView;

@end

@implementation JSModaContentView



- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self didInitIalizeSubView];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame items:(NSArray<SGMModaItem *> *)items{
    if (self = [super initWithFrame:frame]) {
        [self didInitIalizeSubView];
        
        [self addSubview:self.contenView]; // 通过这个方法lazy self.contenView 如果self.contenView 有值 表示是通过自定义item方式进行初始化的
        [self addSubview:self.cancelBtn];
        [self addSubview:self.lineView];
        [self updateContentItems:items];
    }
    return self;
}
- (void)didInitIalizeSubView{
    self.backgroundColor = [UIColor whiteColor];
    self.ba_viewCornerRadius = 5;
    self.ba_viewRectCornerType = BAKit_ViewRectCornerTypeTopLeftAndTopRight;
    _itemContenHeight = 50;
}
- (void)modaDismiss{
    [self.modalViewController hideWithAnimated:YES completion:nil];
}
- (void)updateContentItems:(NSArray<SGMModaItem *> *)items{
    _items = items;
    if (!_contenView) return; // 如果没有值 表示没有通过items 进行初始化
    [self.contenView removeAllSubviews];
    
    for (int i = 0; i < items.count; i ++) {
        SGMModaItem *item = items[i];
        QMUIButton *btn = (QMUIButton *)[QMUIButton buttonWithType:UIButtonTypeCustom]
        .js_normalTitle(item.title)
        .js_textNormalColor(item.titleColor)
        .js_textFont([UIFont fontWithName:PingFangRegularName size:17]);
        if (item.imageName) {
            btn.js_normalImage(UIImageMake(item.imageName));
        }
        btn.tag = i;
        CGFloat x = item.itemPostion == JSModaContentPostionLeft?15:0;
        btn.frame = CGRectMake(x, i * self.itemContenHeight, self.width, self.itemContenHeight);
        [self.contenView addSubview:btn];
        if (item.itemPostion == JSModaContentPostionLeft) {
            btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        }else if (item.itemPostion == JSModaContentPostionCenter){
            btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        }else if (item.itemPostion == JSModaContentPostionRight){
            btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        }
        [btn addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    [self layoutIfNeeded];
}
- (void)setItemContenHeight:(CGFloat)itemContenHeight{
    _itemContenHeight = itemContenHeight;
    [self layoutIfNeeded];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    if (_contenView) { // 在初始化方法中调用后就会有值
        self.contenView.frame = CGRectMake(0, 0, self.width, self.items.count * self.itemContenHeight);
        
        self.cancelBtn.frame = CGRectMake(0, self.height - self.itemContenHeight, self.width, self.itemContenHeight);
        self.lineView.frame = CGRectMake(0, self.cancelBtn.top - 10, self.width, 10);
    }
    
}
- (void)itemClick:(UIButton *)sender{
    if (sender.tag < self.items.count) {
        SGMModaItem *item = self.items[sender.tag];
        if (item.clickHandle) {
            item.clickHandle();
        }
    }
    
    [self modaDismiss];
}
- (UIView *)contenView{
    if (!_contenView) {
        _contenView = [UIView new];
        [self addSubview:_contenView];
    }
    return _contenView;
}
- (QMUIButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = (QMUIButton *)[QMUIButton buttonWithType:UIButtonTypeCustom]
        .js_textFont([UIFont fontWithName:PingFangRegularName size:17])
        .js_normalTitle(@"取消")
        .js_textNormalColor(JS_colorWithHex(0x333333));
        _cancelBtn.tag = 1000;
        [_cancelBtn addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}
- (UIView *)lineView{
    if (!_lineView) {
        _lineView = [UIView new].js_bgColor(JS_colorWithHex(kBodyBGColor));
    }
    return _lineView;
}
@end

@implementation SGMModaItem

- (instancetype)initWithTitle:(NSString *)title imageName:(NSString * _Nullable)imageName itemPostion:(JSModaContentPostion)itemPostion clikeHandle:(dispatch_block_t)clickHandle{
    if (self = [super init]) {
        _title = title;
        _imageName = imageName;
        _itemPostion = itemPostion;
        _clickHandle = [clickHandle copy];
    }
    return self;
}
- (instancetype)initWithTitle:(NSString *)title imageName:(NSString  * _Nullable )imageName titleColor:(UIColor *)titleColor itemPostion:(JSModaContentPostion)itemPostion clikeHandle:(dispatch_block_t)clickHandle{
    if (self = [super init]) {
        _title = title;
        _imageName = imageName;
        _itemPostion = itemPostion;
        _titleColor = titleColor;
        _clickHandle = [clickHandle copy];
    }
    return self;
}
- (UIColor *)titleColor{
    if (!_titleColor) {
        _titleColor = JS_colorWithHex(0x333333);
    }
    return _titleColor;
}
@end


@interface SGMModaBottomView ()

@end

@implementation SGMModaBottomView

- (instancetype)initWithFrame:(CGRect)frame confirmTitle:(nonnull NSString *)confirmTitle compleiton:(nonnull dispatch_block_t)completion{
    if (self = [super initWithFrame:frame]){
        _didSelectConfirm = [completion copy];
        self.js_radius(5);
        QMUIButton *saveBtn = (QMUIButton *)[QMUIButton new]
        .js_textFont(UIFontMake(17))
        .js_textNormalColor(JS_colorWithHex(0x333333))
        .js_normalTitle(confirmTitle)
        .js_bgColor([UIColor whiteColor]);
        QMUIButton *cancel = (QMUIButton *)[QMUIButton new]
        .js_textFont(UIFontMake(17))
        .js_textNormalColor(JS_colorWithHex(0x333333))
        .js_normalTitle(@"取消")
        .js_bgColor([UIColor whiteColor]);
        
        UIView *line = [UIView new].js_bgColor(JS_colorWithHex(kBodyBGColor));
        @weakify(self)
        self.js_addSubViews(@[saveBtn,cancel,line]);
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(0);
            make.right.equalTo(self).offset(0);
            make.centerY.equalTo(self);
            make.height.mas_equalTo(KREALVALUE(10));
        }];
        [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self);
            make.bottom.equalTo(line.mas_top);
        }];
        [cancel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.top.equalTo(line.mas_bottom);
        }];
        [[saveBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            [self modaDismiss];
            if (self.didSelectConfirm) {
                self.didSelectConfirm();
            }
        }];
        [[cancel rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            [self modaDismiss];
        }];
    }
    return self;
}


@end


